package cn.zhong5.changzhoulocaltax.module.web;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.tencent.smtt.export.external.interfaces.SslError;
import com.tencent.smtt.export.external.interfaces.SslErrorHandler;
import com.tencent.smtt.sdk.CookieManager;
import com.tencent.smtt.sdk.CookieSyncManager;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import cn.zhong5.changzhoulocaltax.R;
import cn.zhong5.changzhoulocaltax.base.BaseActivity;
import cn.zhong5.changzhoulocaltax.common.constants.ConstantValues;
import cn.zhong5.changzhoulocaltax.common.utils.NetworkUtils;
import cn.zhong5.changzhoulocaltax.common.utils.ToastUtils;
import cn.zhong5.changzhoulocaltax.common.utils.ToolbarHelper;

/**
 * Created by Sunman on 2017-12-4 0004.
 */

public class WebViewActivity extends BaseActivity
{

    @BindView(R.id.mainActivity)
    LinearLayout mainActivity;

    @BindView(R.id.web_swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;

    @BindView(R.id.forum_context)
    X5WebView mWebView;

    private String mUrl = "";
    @Override
    protected int getContentViewLayoutID()
    {
        return R.layout.activity_web;
    }

    @Override
    protected void initToolbar(ToolbarHelper toolbarHelper) {
        toolbarHelper.hideLeftView();
        toolbarHelper.hideRightView();
        toolbarHelper.setTitle(getString(R.string.app_name));
    }


    @Override
    protected void initView(Bundle savedInstanceState)
    {
        mSwipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));//Color.rgb(47, 223, 189));
        mSwipeRefreshLayout.setEnabled(false);

        mWebView.setWebChromeClient(new MyWebChrome());
        mWebView.setWebViewClient(new MyWebClient());

        mUrl = ConstantValues.BASE_SERVER_URL;
        if (NetworkUtils.isNetworkConnectivity(this)) {
            loadWebUrl();
        } else {
            ToastUtils.showError("网络连接错误");
        }
    }

    private void loadWebUrl() {
        Map<String, String> extraHeaders = new HashMap<String, String>();
        extraHeaders.put("name", "viewport");
        extraHeaders.put("content", "width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no");
        if(null != mUrl && mUrl.length() > 0)
        {
            mWebView.loadUrl(mUrl, extraHeaders);
        }

    }

    @Override
    public void onResume()
    {
        super.onResume();
        mWebView.loadUrl(mUrl);
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        CookieSyncManager.createInstance(this);
        CookieManager.getInstance().removeAllCookie();
    }

    // 保存用户按返回键的时间
    private long mExitTime = 0;
    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - mExitTime) > 2000)
        {
            Snackbar.make(mainActivity, R.string.exit_toast, Toast.LENGTH_SHORT).show();
            mExitTime = System.currentTimeMillis();
        } else
        {
            finish();
        }
    }

    private class MyWebChrome extends WebChromeClient
    {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            mSwipeRefreshLayout.setRefreshing(true);
        }
    }

    private class MyWebClient extends WebViewClient
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            //Log.e("Test", "url....=" + url);
            return true;
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            super.onReceivedSslError(view, handler, error);
            handler.proceed();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }
}
